package de.tapps.brainstorming;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class GUI {
	private MainActivity activity = new MainActivity();
	private Storage storage;
	AlertDialog.Builder alert;

	public GUI(MainActivity act){
		activity = act;
		storage = new Storage(activity);

	}
	public void showMessage(String pmessage){
		Toast.makeText(activity, pmessage, Toast.LENGTH_LONG).show();
	}
	public void backButton(){
		if (activity.positionInProgram.equals("menu")){
			AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
					activity);
	 
				alertDialogBuilder.setTitle(activity.getResources().getString(R.string.quitapp));
	 
				// set dialog message
				alertDialogBuilder
					.setMessage(activity.getResources().getString(R.string.quitappquestion))
					.setCancelable(false)
					.setPositiveButton(activity.getResources().getString(R.string.quitbutton),new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) {
							// if this button is clicked, close
							// current activity
							activity.finish();
						}
					  })
					.setNegativeButton(activity.getResources().getString(R.string.cancelbutton),new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog,int id) {
							// if this button is clicked, just close
							// the dialog box and do nothing
							dialog.cancel();
						}
					});
	 
					// create alert dialog
					AlertDialog alertDialog = alertDialogBuilder.create();
	 
					// show it
					alertDialog.show();
				}
	
		
		else if (activity.positionInProgram.equals("languages")){
			activity.positionInProgram = "menu";
			String[] menu = {
					activity.getResources().getString(R.string.newtopic),
					activity.getResources().getString(R.string.newanswer),
					activity.getResources().getString(R.string.language),
					//getResources().getString(R.string.settings),
					activity.getResources().getString(R.string.help),
					activity.getResources().getString(R.string.credits) };
			
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
					R.layout.custom_list, menu);
			activity.setListAdapter(adapter);
		}
		else if (activity.positionInProgram.equals("newanswers")){
			activity.positionInProgram = "menu";
			String[] menu = {
					activity.getResources().getString(R.string.newtopic),
					activity.getResources().getString(R.string.newanswer),
					activity.getResources().getString(R.string.language),
					//getResources().getString(R.string.settings),
					activity.getResources().getString(R.string.help),
					activity.getResources().getString(R.string.credits) };
			
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
					R.layout.custom_list, menu);
			activity.setListAdapter(adapter);

			 activity.findViewById(R.id.spinner_order).setVisibility(View.GONE);
			 activity.findViewById(R.id.orderby).setVisibility(View.GONE);
			 ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) activity.getListView()
				        .getLayoutParams();
			 int pixelsValue = 0; // margin in pixels
			 float d = activity.getBaseContext().getResources().getDisplayMetrics().density;
			 int margin = (int)(pixelsValue * d);
				mlp.setMargins(0, margin, 0, 0);
			
			


			activity.is_topiclist = false;
		}
		else if (activity.positionInProgram.equals("newtopics")){
			activity.positionInProgram = "menu";
			(activity.findViewById(R.id.startnewtopic)).setVisibility(View.GONE);
			(activity.findViewById(R.id.topic)).setVisibility(View.GONE);
			(activity.findViewById(R.id.inputtopic)).setVisibility(View.GONE);
			(activity.findViewById(R.id.topicdescr)).setVisibility(View.GONE);
			(activity.findViewById(R.id.inputdescr)).setVisibility(View.GONE);
			(activity.findViewById(R.id.topic_count)).setVisibility(View.GONE);
			(activity.findViewById(R.id.topicdescr_count)).setVisibility(View.GONE);
			String[] menu = {
					activity.getResources().getString(R.string.newtopic),
					activity.getResources().getString(R.string.newanswer),
					activity.getResources().getString(R.string.language),
					//getResources().getString(R.string.settings),
					activity.getResources().getString(R.string.help),
					activity.getResources().getString(R.string.credits) };
			
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
					R.layout.custom_list, menu);
			activity.setListAdapter(adapter);


			
		}
	
		else if (activity.positionInProgram.equals("chosentopic")){
			activity.positionInProgram = "newanswers";
			activity.findViewById(R.id.spinner_order).setVisibility(View.VISIBLE);
			 activity.findViewById(R.id.orderby).setVisibility(View.VISIBLE);
			
			
			activity.findViewById(R.id.answer_topic).setVisibility(View.GONE);
			activity.findViewById(R.id.answer_topicdescr).setVisibility(View.GONE);
			activity.findViewById(R.id.new_answer).setVisibility(View.GONE);
			activity.findViewById(R.id.new_answer_input).setVisibility(View.GONE);
			activity.findViewById(R.id.new_answer_button).setVisibility(View.GONE);
			activity.findViewById(R.id.new_answer_count).setVisibility(View.GONE);

			
			activity.findViewById(R.id.line1).setVisibility(View.GONE);
			activity.findViewById(R.id.line2).setVisibility(View.GONE);
			activity.is_answerlist = false;
			
			

    		activity.setListAdapter(null);
			activity.findViewById(R.id.spinner_order).setVisibility(View.VISIBLE);
			 activity.findViewById(R.id.orderby).setVisibility(View.VISIBLE);
			 ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) activity.getListView()
				        .getLayoutParams();
			 int pixelsValue = 60; // margin in pixels
			 float d = activity.getBaseContext().getResources().getDisplayMetrics().density;
			 int margin = (int)(pixelsValue * d);
				mlp.setMargins(0, margin, 0, 0);
			
			

			new TopicList(activity).execute();

			activity.is_topiclist = true;
		
		
	
		}
		else if (activity.positionInProgram.equals("credits")){
			activity.positionInProgram = "menu";
			activity.findViewById(R.id.tapps_logo).setVisibility(View.GONE);
			activity.findViewById(R.id.publishing_mail).setVisibility(View.GONE);
			activity.findViewById(R.id.publishing_text).setVisibility(View.GONE);
			String[] menu = {
					activity.getResources().getString(R.string.newtopic),
					activity.getResources().getString(R.string.newanswer),
					activity.getResources().getString(R.string.language),
					//getResources().getString(R.string.settings),
					activity.getResources().getString(R.string.help),
					activity.getResources().getString(R.string.credits) };
			
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
					R.layout.custom_list, menu);
			activity.setListAdapter(adapter);
		}
		
	}
	public void reportAnswer(int position){
		activity.currentTopicID = activity.topicIDs[position];
	alert = new AlertDialog.Builder(activity);
	alert.setTitle(R.string.spamreport);
	alert.setMessage(R.string.spamquestion);
	alert.setIcon(R.drawable.spambutton);
	alert.setPositiveButton(R.string.spambutton,
			new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
						int whichButton) {
					new ReportAnswer(activity,activity.currentTopicID,0).execute();
					showMessage(activity.getResources().getString(R.string.successfulreport));
				}
});
	alert.setNegativeButton(R.string.cancelbutton,
			new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
						int whichButton) {
					// Canceled.
				}
			});

	}
	
	public void isAdmin(){
		

	if (activity.isAdmin){
		alert.setNeutralButton(R.string.deletetopic,
				new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface diaog,
							int whichButton){
						TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
						String imei = telephonyManager.getDeviceId();
						new DeleteEntry(activity,activity.currentTopicID,0,imei).execute();
						showMessage(activity.getResources().getString(R.string.deletetopic).toString());
					}
		});

		}
	alert.show();				

	}
		public void generateMenu(){
			String[] menu = {
					activity.getResources().getString(R.string.newtopic),
					activity.getResources().getString(R.string.newanswer),
					activity.getResources().getString(R.string.language),
					//getResources().getString(R.string.settings),
					activity.getResources().getString(R.string.help),
					activity.getResources().getString(R.string.credits) };

			ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
					R.layout.custom_list, menu);
			activity.setListAdapter(adapter);
		}
		public void registerListener(){
			activity.startnewtopic = (Button)activity.findViewById(R.id.startnewtopic);
			activity.startnewtopic.setOnClickListener(activity);
			//activity.newanswer = (Button)activity.findViewById(R.id.new_answer);
					//activity.newanswer.setOnClickListener(activity);
			Spinner spinner = (Spinner) activity.findViewById(R.id.spinner_order);
			spinner.setOnItemSelectedListener(activity);
			ListView list = activity.getListView();
			TextWatcher watcher = new TextWatcher() {
			    @Override
			    public void onTextChanged(CharSequence charsequence, int i, int j, int k) {
			    	EditText newAnswer = (EditText)activity.findViewById(R.id.new_answer_input);
			    	String input = newAnswer.getText().toString();
			    	int inputl = input.length();
			        ((TextView)activity.findViewById(R.id.new_answer_count)).setText(activity.getResources().getString(R.string.remaining_signs)+(125-inputl));       
			        EditText inputtopic = (EditText)activity.findViewById(R.id.inputtopic);
			    	String inputt = inputtopic.getText().toString();
			    	int inputtl = inputt.length();
			        ((TextView)activity.findViewById(R.id.topic_count)).setText(activity.getResources().getString(R.string.remaining_signs)+(100-inputtl));   
			        EditText inputtopicd = (EditText)activity.findViewById(R.id.inputdescr);
			    	String inputtd = inputtopicd.getText().toString();
			    	int inputtdl = inputtd.length();
			        ((TextView)activity.findViewById(R.id.topicdescr_count)).setText(activity.getResources().getString(R.string.remaining_signs)+(125-inputtdl));        

			    }

				@Override
				public void afterTextChanged(Editable arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
						int arg3) {
					// TODO Auto-generated method stub
					
				}
			    
			  
			};
			((EditText)activity.findViewById(R.id.new_answer_input)).addTextChangedListener(watcher);
			((EditText)activity.findViewById(R.id.inputtopic)).addTextChangedListener(watcher);
			((EditText)activity.findViewById(R.id.inputdescr)).addTextChangedListener(watcher);


			list.setOnItemLongClickListener (activity);
					
		}
		public void onClickStartnewtopic(){
			String topic = ((EditText) activity.findViewById(R.id.inputtopic)).getText()
					.toString();
			String topicdesc = ((EditText) activity.findViewById(R.id.inputdescr))
					.getText().toString();
			if (!topic.equals("") && !topicdesc.equals("")) {
				new CreateTopic(activity,topic, topicdesc, "").execute();
				((EditText) activity.findViewById(R.id.inputtopic)).setText("");
				((EditText) activity.findViewById(R.id.inputdescr)).setText("");
				showMessage(activity.getResources().getString(R.string.createdNewTopic));
			} else if (topic.equals("")) {
				showMessage(activity.getResources().getString(R.string.errorNoTopic));
			} else if (topicdesc.equals("")) {
				showMessage(activity.getResources().getString(R.string.errorNoTopicDescr));
			}
		}
	public void onClickNewanswer(){
		String answer = ((EditText) activity.findViewById(R.id.new_answer_input)).getText()
				.toString();
		if (!answer.equals("")){
			new CreateAnswer(activity,""+activity.currentTopicID, answer, "").execute();
			

		}
		else {
			showMessage(activity.getResources().getString(R.string.errorNoTopic));
		}

		((EditText)activity.findViewById(R.id.new_answer_input)).setText("");
	}
	
	private Runnable mGetLanguage = new Runnable() {   
        public void run() { 
        	for (int i = 0; i < activity.getListView().getChildCount(); i++)
			{
        				
			    TextView tx = (TextView) activity.getListView().getChildAt(i);
			    if (tx.getText().equals("Deutsch") && storage.getLanguage().equals("De")){
tx.setBackgroundColor(Color.parseColor("#40ffffff"));
}
			    else if (tx.getText().equals("English") && storage.getLanguage().equals("En")){
			    	tx.setBackgroundColor(Color.parseColor("#40ffffff"));
			    }
			    else if (tx.getText().equals("Espa�ol") && storage.getLanguage().equals("Es")){
			    	tx.setBackgroundColor(Color.parseColor("#40ffffff"));
			    }
			    else if (tx.getText().equals("Fran�ais") && storage.getLanguage().equals("Fr")){
			    	tx.setBackgroundColor(Color.parseColor("#40ffffff"));
			    }
			    else if (tx.getText().equals("Italiano") && storage.getLanguage().equals("It")){
			    	tx.setBackgroundColor(Color.parseColor("#40ffffff"));

			    }
        }}};
public void position_chosenTopic(int position){
	activity.positionInProgram = "chosentopic";
	activity.findViewById(R.id.spinner_order).setVisibility(View.GONE);
	activity.findViewById(R.id.orderby).setVisibility(View.GONE);
	 ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) activity.getListView()
		        .getLayoutParams();
	 int pixelsValue = 0; // margin in pixels
	 float d = activity.getBaseContext().getResources().getDisplayMetrics().density;
	 int margin = (int)(pixelsValue * d);
		mlp.setMargins(0, margin, 0, 0);
	String[] empty = new String[] { "" };
	
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
			R.layout.custom_list, empty);
	activity.setListAdapter(adapter);
	
	activity.currentTopicID = activity.topicIDs[position];
	
	new GetTopic(activity,activity.topicIDs[position]).execute();
	new AnswerList(activity,activity.topicIDs[position]).execute();
	activity.findViewById(R.id.answer_topic).setVisibility(View.VISIBLE);
	activity.findViewById(R.id.answer_topicdescr).setVisibility(View.VISIBLE);
	activity.findViewById(R.id.new_answer).setVisibility(View.VISIBLE);
	activity.findViewById(R.id.new_answer_input).setVisibility(View.VISIBLE);
	activity.findViewById(R.id.new_answer_button).setVisibility(View.VISIBLE);
	activity.findViewById(R.id.new_answer_count).setVisibility(View.VISIBLE);

	activity.newanswer = (Button)activity.findViewById(R.id.new_answer_button);
	activity.newanswer.setOnClickListener(activity);
	
	activity.findViewById(R.id.line1).setVisibility(View.VISIBLE);
	activity.findViewById(R.id.line2).setVisibility(View.VISIBLE);
	activity.is_answerlist = true;
	
	activity.is_topiclist = false;
}
public void positionIsAnswerlist(int clickPosition){
	activity.currentAnswerID = activity.answerIDs[clickPosition];

	AlertDialog.Builder alert = new AlertDialog.Builder(activity);
	alert.setTitle(R.string.spamreport);
	alert.setMessage(R.string.spamquestion);
	alert.setIcon(R.drawable.spambutton);
	alert.setPositiveButton(R.string.spambutton,
			new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
						int whichButton) {
					new ReportAnswer(activity,activity.currentTopicID,activity.currentAnswerID).execute();
					showMessage(activity.getResources().getString(R.string.successfulreport));
					// Enter Spam Entry to Database
				}
			});
	alert.setNegativeButton(R.string.cancelbutton,
			new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog,
						int whichButton) {
					// Canceled.
				}
			});
	if (activity.isAdmin){
		alert.setNeutralButton(R.string.deleteanswer,
				new DialogInterface.OnClickListener(){
					public void onClick(DialogInterface diaog,
							int whichButton){
						// topicID, answerID, imei
						TelephonyManager telephonyManager = (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
						String imei = telephonyManager.getDeviceId();
						new DeleteEntry(activity,activity.currentTopicID,activity.currentAnswerID,imei).execute();
						showMessage(activity.getResources().getString(R.string.deleteanswer).toString());
					}
		});
	alert.show();
}
}
public void positionCredits(){
	activity.positionInProgram = "credits";
	activity.findViewById(R.id.tapps_logo).setVisibility(View.VISIBLE);
	activity.findViewById(R.id.publishing_mail).setVisibility(View.VISIBLE);
	activity.findViewById(R.id.publishing_text).setVisibility(View.VISIBLE);
	String[] values = new String[] {""};
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
			R.layout.custom_list, values);
	activity.setListAdapter(adapter);
}
public void positionLanguage(){
	activity.positionInProgram = "languages";
	
	String[] languages = new String[] { "Deutsch", "English",
			"Espa�ol", "Fran�ais", "Italiano" };
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
			R.layout.custom_list, languages);
	activity.setListAdapter(adapter);
	Handler mHandlerTimer = new Handler();
				mHandlerTimer.removeCallbacks(mGetLanguage);            
	mHandlerTimer.postDelayed(mGetLanguage, 500);
}
public void languageDE(){
	storage.setLanguage("De");
	String[] values = new String[] {
			activity.getResources().getString(R.string.newtopic),
			activity.getResources().getString(R.string.newanswer),
			activity.getResources().getString(R.string.language),
			//getResources().getString(R.string.settings),
			activity.getResources().getString(R.string.help),
			activity.getResources().getString(R.string.credits) };
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
			R.layout.custom_list, values);
	activity.setListAdapter(adapter);
}
public void languageEN(){
	storage.setLanguage("En");
	String[] values = new String[] {
			activity.getResources().getString(R.string.newtopic),
			activity.getResources().getString(R.string.newanswer),
			activity.getResources().getString(R.string.language),
		//	getResources().getString(R.string.settings),
			activity.getResources().getString(R.string.help),
			activity.getResources().getString(R.string.credits) };
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
			R.layout.custom_list, values);
	activity.setListAdapter(adapter);
}
public void languageES(){
	storage.setLanguage("Es");
	String[] values = new String[] {
			activity.getResources().getString(R.string.newtopic),
			activity.getResources().getString(R.string.newanswer),
			activity.getResources().getString(R.string.language),
			//getResources().getString(R.string.settings),
			activity.getResources().getString(R.string.help),
			activity.getResources().getString(R.string.credits) };
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
			R.layout.custom_list, values);
	activity.setListAdapter(adapter);
}
public void languageFR(){
	storage.setLanguage("Fr");
	String[] values = new String[] {
			activity.getResources().getString(R.string.newtopic),
			activity.getResources().getString(R.string.newanswer),
			activity.getResources().getString(R.string.language),
		//	getResources().getString(R.string.settings),
			activity.getResources().getString(R.string.help),
			activity.getResources().getString(R.string.credits) };
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
			R.layout.custom_list, values);
	activity.setListAdapter(adapter);
}
public void languageIT(){
	storage.setLanguage("It");
	String[] values = new String[] {
			activity.getResources().getString(R.string.newtopic),
			activity.getResources().getString(R.string.newanswer),
			activity.getResources().getString(R.string.language),
		//	getResources().getString(R.string.settings),
			activity.getResources().getString(R.string.help),
			activity.getResources().getString(R.string.credits) };
	ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
			R.layout.custom_list, values);
	activity.setListAdapter(adapter);
}
public void positionNewAnswers(){
	activity.positionInProgram = "newanswers";
	
	    		activity.setListAdapter(null);
	    		activity.findViewById(R.id.spinner_order).setVisibility(View.VISIBLE);
	    		activity.findViewById(R.id.orderby).setVisibility(View.VISIBLE);
				 ViewGroup.MarginLayoutParams mlp = (ViewGroup.MarginLayoutParams) activity.getListView()
					        .getLayoutParams();
				 int pixelsValue = 60; // margin in pixels
				 float d = activity.getBaseContext().getResources().getDisplayMetrics().density;
				 int margin = (int)(pixelsValue * d);
					mlp.setMargins(0, margin, 0, 0);
				
				

				new TopicList(activity).execute();

				activity.is_topiclist = true;
}
public void positionNewTopics(){
	activity.setListAdapter(null);
	activity.positionInProgram = "newtopics";
	(activity.findViewById(R.id.startnewtopic)).setVisibility(View.VISIBLE);
	(activity.findViewById(R.id.topic)).setVisibility(View.VISIBLE);
	(activity.findViewById(R.id.inputtopic)).setVisibility(View.VISIBLE);
	(activity.findViewById(R.id.topicdescr)).setVisibility(View.VISIBLE);
	(activity.findViewById(R.id.inputdescr)).setVisibility(View.VISIBLE);
	(activity.findViewById(R.id.topic_count)).setVisibility(View.VISIBLE);
	(activity.findViewById(R.id.topicdescr_count)).setVisibility(View.VISIBLE);
}
}
